#!/usr/local/bin/php
<?php
defined('OPEN_LIB_PATH') || define('OPEN_LIB_PATH', '/opt/op/OpenLib');

require_once OPEN_LIB_PATH . '/open/models/OaAppModel.php';
require_once OPEN_LIB_PATH . '/open/models/OaCodeModel.php';

$logger = Log::factory('console', '', '', array(
    'lineFormat' => '[%1$s] [%3$s] %5$s(%6$s): %4$s',
    'timeFormat' => '%Y-%m-%d %H:%M:%S'
), PEAR_LOG_INFO);

$codeModel = new OaCodeModel();
$operations = array(1, 2, 3, 4);
$envs = array(OaAppModel::APP_CONFIG_ENV_TEST, OaAppModel::APP_CONFIG_ENV_PRODUCTION);
$custs = array(true, false);
foreach ($operations as $operation) {
	foreach ($envs as $env) {
		foreach ($custs as $cust) {
	        $xml = $codeModel->unicode($operation, $env, $cust);
	        if ($xml === false) {
	        	$logger->warning("unicode failed: operation-$operation, env-$env, cust-$cust");
	        	continue;
	        }
	        $filename = "/uploads/open/cache/unicode_{$operation}_{$env}" . 
	        	($cust ? '_cust' : '') . ".xml";
	        if (!file_put_contents($filename, $xml)) {
	        	$logger->warning("file_put_contents failed: file-$filename");
	        	continue;
	        }
	        exec("chown nobody:nobody $filename");
	        $logger->info("refresh succeed: operation-$operation, env-$env, cust-$cust");
		}
	}
}
